package zhoug.map;

import android.graphics.PointF;
import android.view.MotionEvent;

import zhoug.map.location.ZLatLng;

import org.osmdroid.api.IGeoPoint;
import org.osmdroid.views.MapView;
import org.osmdroid.views.Projection;
import org.osmdroid.views.overlay.Overlay;

/**
 * 地图事件监听定义
 * @Author: zhoug
 * @Date: 2024-12-31
 * @Description:
 */
public class EventOverlay extends Overlay {
    /**
     * 单击地图监听
     */
    private OnClickMapListener onClickMapListener;
    /**
     * 是否触摸在地图上
     */
    private boolean mIsTouched;

    public EventOverlay() {
        super();
    }


    @Override
    public boolean onTouchEvent(MotionEvent event, MapView mapView) {
        int action = event.getAction();
        if (action == MotionEvent.ACTION_DOWN) {
            mIsTouched = true;
        } else if (action == MotionEvent.ACTION_UP) {
            mIsTouched = false;
        }
        return super.onTouchEvent(event, mapView);
    }


    @Override
    public boolean onSingleTapConfirmed(MotionEvent e, MapView mapView) {
        if (onClickMapListener != null) {
            Projection projection = mapView.getProjection();
            ZLatLng latLng=null;
            PointF point=new PointF(e.getX(),e.getY());
            if(projection!=null){
                IGeoPoint geoPoint = projection.fromPixels((int) point.x, (int) point.y);
                if(geoPoint!=null){
                    latLng=new ZLatLng(geoPoint.getLatitude(),geoPoint.getLongitude());
                }
            }
            return onClickMapListener.onClickMap(latLng,point);
        }
        return super.onSingleTapConfirmed(e, mapView);
    }

    public void setOnClickMapListener(OnClickMapListener onClickMapListener) {
        this.onClickMapListener = onClickMapListener;
    }

    public boolean isTouched(){
        return mIsTouched;
    }

}
